<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-row :gutter="35">

					<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb20">
						<el-form-item label="">
							<el-input placeholder="请输入管理员信息" v-model="state.tableData.param.keywords" class="w100" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-button size="default" type="" class="ml10" @click="onSearchSubmit()">
						<el-icon><ele-Search/></el-icon>
						查询
					</el-button>

					<el-button size="default" type="primary" plain class="ml10" v-auth="'/api/admin/permission/system_user_form_submit'" @click="onOpenAddUser('add')">
						<el-icon>
							<ele-FolderAdd />
						</el-icon>
						新增管理员
					</el-button>

				</el-row>
			</div>
			<el-table
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
				border size="small"
				:header-cell-style="{backgroundColor: '#f8f8f8', color: '#010101'}"
				highlight-current-row
				style="width: 100%"
            >
				<el-table-column type="index" label="序号" width="80" />
				<el-table-column prop="username" label="管理员账户" show-overflow-tooltip></el-table-column>
				<el-table-column prop="nickname" label="管理员昵称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="avatar" label="管理员头像" show-overflow-tooltip>
                    <template #default="scope">
                        <el-image
		                    style="width: 50px; height: 50px; border-radius: 50%"
		                    :src="scope.row.avatar"
		                    :preview-src-list="[scope.row.avatar]"
		                    :preview-teleported="true"
		                    :hide-on-click-modal="true"
		                    fit="cover"
	                    />
                    </template>
                </el-table-column>
				<el-table-column prop="status" label="管理员状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="role.role_name" label="角色" show-overflow-tooltip></el-table-column>
				<el-table-column prop="login_num" label="登录次数" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="default">{{ scope.row.login_num }}次</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="login_last_time" label="上次登录时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="created_at" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'/lxs/saas/power/user/user_form_submit'" @click="onOpenEditUser('edit', scope.row)"
							>修改</el-button
						>
						<el-button :disabled="scope.row.username === 'maite'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<UserDialog ref="userDialogRef" @refresh="getTableData()" :roleList="roleList" />
	</div>
</template>

<script setup name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useUserApi } from "/src/api/permission/user";
import { useRoleApi } from "/src/api/permission/role";

// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/permission/user/dialog.vue'));

// 定义变量内容
const userDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			page: 1,
            pageSize: 10,
			keywords: '',
		},
	},
});

const roleList = ref([]);
const getRoleList = () => {
	useRoleApi().getRoleSelect({}).then((res) => {
        roleList.value = res.data;
    })
}

// 搜索
const onSearchSubmit = async () => {
	state.tableData.loading = true;

	await useUserApi().getAdminListData(state.tableData.param).then((res) => {
		state.tableData.data = res.data.list.data;
		state.tableData.total = res.data.list.total;

		state.tableData.loading = false;
	});
};

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;

    await useUserApi().getAdminListData(state.tableData.param).then((res) => {
	    state.tableData.data = res.data.list.data;
	    state.tableData.total = res.data.list.total;

	    state.tableData.loading = false;
    })
};
// 打开新增管理员弹窗
const onOpenAddUser = (type) => {
	userDialogRef.value.openDialog(type);
};
// 打开修改管理员弹窗
const onOpenEditUser = (type, row) => {
	userDialogRef.value.openDialog(type, row);
};
// 删除管理员
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.username}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(async () => {
		await useUserApi().adminDeleteSubmit({id: row}).then((res) => {
			if (res.code === 200) {
				getTableData();
				ElMessage.success(res.message);
			} else {
				ElMessage.error(res.message);
			}
		})
	})
	.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
    getRoleList();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
